package cn.hzu580.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.opensymphony.xwork2.ActionContext;

import cn.hzu580.dao.BaseDAO;
import cn.hzu580.entity.Notice;
import cn.hzu580.service.NoticeService;
import cn.hzu580.vo.NoticeVO;

@Service("notice")
public class NoticeServiceImpl implements NoticeService {

	@Resource(name="DAO")
	private BaseDAO baseDAO;
	
	
	@Override
	public List<Notice> getAll() {
		Map session = ActionContext.getContext().getSession();
		Map<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("owner_uid", session.get("uid"));
		List<Notice> list = (List<Notice>) baseDAO.exeQuery("from Notice where owner.uid = :owner_uid and has_delete = false", parameters);
		return list;
	}

	@Override
	public void read(NoticeVO vo) {
		Notice notice = (Notice) baseDAO.get(Notice.class, vo.getId());
		//判断通知的所有者
		if(!ActionContext.getContext().getSession().get("uid").equals(notice.getOwner().getUid())){
			throw new RuntimeException("message's owner is not you");
		}
		notice.setHas_read(true);
		baseDAO.update(notice);
	}

	@Override
	public void readAll() {
		Map session = ActionContext.getContext().getSession();
		Map<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("owner_uid", session.get("uid"));
		baseDAO.exeUpdate("update Notice set has_read = true where owner.uid = :owner_uid", parameters);
	}

	@Override
	public void clear() {
		Map session = ActionContext.getContext().getSession();
		Map<String, Object> parameters = new HashMap<String , Object>();
		parameters.put("owner_uid", session.get("uid"));
		baseDAO.exeUpdate("update Notice set has_delete = true where owner.uid = :owner_uid", parameters);
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	
	
	
}
